﻿using Modbus.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace PriterApp
{
    public class ModbusRtuPlc
    {
        public static IModbusMaster master;

        public static ushort[] ReadHoldingRegister(byte slave, ushort startAdd, ushort num)
        {
            //try
            //{

            return master.ReadHoldingRegisters(slave, startAdd, num);


        }


        public static bool[] ReadCoil(byte slave, ushort startAdd, ushort num)
        {
            //try
            //{

            return master.ReadCoils(slave, startAdd, num);


        }


        public static void WriteSingleCoil(byte slave, ushort startAdd, bool num)
        {
            try
            {
                master.WriteSingleCoil(slave, startAdd, num);
            }
            catch
            { }




        }

        /// <summary>
        /// 十六进制换算为十进制
        /// </summary>
        /// <param name="strColorValue"></param>
        /// <returns></returns>
        public static int GetHexadecimalValue(String strColorValue)
        {
            char[] nums = strColorValue.ToCharArray();
            int total = 0;
            try
            {
                for (int i = 0; i < nums.Length; i++)
                {
                    String strNum = nums[i].ToString().ToUpper();
                    switch (strNum)
                    {
                        case "A":
                            strNum = "10";
                            break;
                        case "B":
                            strNum = "11";
                            break;
                        case "C":
                            strNum = "12";
                            break;
                        case "D":
                            strNum = "13";
                            break;
                        case "E":
                            strNum = "14";
                            break;
                        case "F":
                            strNum = "15";
                            break;
                        default:
                            break;
                    }
                    double power = Math.Pow(16, Convert.ToDouble(nums.Length - i - 1));
                    total += Convert.ToInt32(strNum) * Convert.ToInt32(power);
                }
            }
            catch (System.Exception ex)
            {
                String strErorr = ex.ToString();
                return 0;
            }
            return total;
        }
    }


    public class PlcDataClass
    {
        public bool StartCheck = false;
        public bool EndCheck = false;

       

        public bool DianzuOK = false;
        public bool DianzuNG = false;

        public bool NaiyaOK = false;
        public bool NaiyaNG = false;

        public bool PressOK = false;
        public bool PressNG = false;

        public bool ShiftOK = false;
        public bool ShiftNG = false;

        public string DianzuValue = string.Empty;
        public string PressValue = string.Empty;
        public string ShiftValue = string.Empty;

    }
}
